package com.dpj.netty.c2.future;

import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;

@Slf4j
public class TestNettyPromise {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //1.准备EventLoop对象
        EventLoop eventLoop = new NioEventLoopGroup().next();
        //1.可以主动创建promise对象, 结果容器
        DefaultPromise<Integer> promise=new DefaultPromise<>(eventLoop);

        new Thread(()->{
            try {

                //任意一个线程执行计算，计算完毕后向promise中添加结果
                log.debug("开始计算");
                int i=1/0;
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
                promise.setFailure(e);
            }
            promise.setSuccess(80);
        }).start();

        log.debug("等待结果");
        log.debug("结果是{}",promise.get());
    }
}
